package xj.graph2d.tree;

import java.util.Map;

import javax.swing.*;

import xj.graph2d.CompoundDrawShape;
import xj.graph2d.Document;
import xj.graph2d.DrawShape;
import xj.graph2d.EllipseShape;
import xj.graph2d.LineShape;
import xj.graph2d.RectangleShape;
import xj.graph2d.TextShape;
import xj.graph2d.app.project.ProjectItem;
import xj.graph2d.shapes.BoxLabel;
import xj.graph2d.shapes.CircleShape;
import xj.graph2d.shapes.CubicCurveShape;
import xj.graph2d.shapes.OrthogonalLineShape;
import xj.graph2d.shapes.PolyCurveShape;
import xj.graph2d.shapes.PolyLineShape;
import xj.graph2d.shapes.PolygonShape;
import xj.graph2d.shapes.QuadCurveShape;
import xj.graph2d.shapes.RectangularShape;
import xj.graph2d.shapes.RoundRectangleShape;
import xj.graph2d.shapes.ScribbleShape;
import xj.graph2d.shapes.ShapeIcon;

public class DocumentTreeCellIcons {  

  protected Icon homeIcon;
  protected Icon docIcon;
  protected Icon projIcon;
  protected Icon umlProjIcon;
  protected Icon folderIcon;
  protected Icon lineIcon;
  protected Icon arcIcon;
  protected Icon curveIcon;
  protected Icon filledRectIcon;
  protected Icon rectIcon;
  protected Icon filledRoundRectIcon;
  protected Icon roundRectIcon;
  protected Icon ovalIcon;
  protected Icon filledOvalIcon;
  protected Icon circleIcon;
  protected Icon polygonIcon;
  protected Icon polylineIcon;
  protected Icon scribbleIcon;
  protected Icon textIcon;
  protected Icon polycurveIcon;
  protected Icon orthlineIcon;
  protected Icon imageIcon;

  protected Icon classDiagramIcon;
  protected Icon packageDiagramIcon;
  protected Icon useCaseDiagramIcon;
  protected Icon sequenceDiagramIcon;
  protected Icon stateDiagramIcon;
  protected Icon activityDiagramIcon;

  protected Map<String, Icon> icons;

  public DocumentTreeCellIcons(Map<String, Icon> icons) {
    this.icons = icons;
    if (icons != null) {
      homeIcon = icons.get("homeIcon");
      projIcon = icons.get("projIcon");
      umlProjIcon = icons.get("UMLLogo");
      docIcon = icons.get("docIcon");
      folderIcon = icons.get("folderIcon");

      classDiagramIcon = icons.get("UMLClassDiagram.Class1"); 
      packageDiagramIcon = icons.get("UMLPackageDiagram.Package1");
      useCaseDiagramIcon = icons.get("UMLUseCaseDiagram.UseCase");
      sequenceDiagramIcon = icons.get("UMLSequenceDiagram.Object");
      stateDiagramIcon = icons.get("UMLStateDiagram.StateMachine");
      activityDiagramIcon = icons.get("UMLActivityDiagram.SendEvent");

      lineIcon = icons.get("lineIcon");
      arcIcon = icons.get("arcIcon");
      curveIcon = icons.get("curveIcon");
      filledRectIcon = icons.get("filledRectIcon");
      rectIcon = icons.get("rectIcon");
      filledRoundRectIcon = icons.get("filledRoundRectIcon");
      roundRectIcon = icons.get("roundRectIcon");
      ovalIcon = icons.get("ovalIcon");
      filledOvalIcon = icons.get("filledOvalIcon");
      circleIcon = icons.get("circleIcon");
      polygonIcon = icons.get("polygonIcon");
      polylineIcon = icons.get("polylineIcon");
      scribbleIcon = icons.get("scribbleIcon");
      textIcon = icons.get("jdeTextIcon");
      polycurveIcon = icons.get("polycurveIcon");
      orthlineIcon = icons.get("orthlineIcon");
      imageIcon = icons.get("imageIcon");
    }
  }

  public Icon getTreeCellIcon(Object value) { 
    if (value instanceof DocumentTreeNode) {
      return docIcon;
    } else if (value instanceof GraphTreeNode) {
      return folderIcon;
    } else if (value instanceof ProjectTreeNode) {
      ProjectTreeNode projnode = (ProjectTreeNode) value;
      int t = projnode.getNodeType();
      switch (t) {
      case ProjectTreeNode.UML_PROJECT:
	return projIcon;
      case ProjectTreeNode.PROJECT:
	return projIcon;
      case ProjectTreeNode.PROJECT_FOLDER:
	return folderIcon;
      case ProjectTreeNode.PROJECT_ITEM:
	ProjectItem item = projnode.getProjectItem();
	Document.DocumentType doctype = item.getItemType();
	switch (doctype) { 
	case UML_Class_Diagram:
	  return classDiagramIcon;
	case UML_Package_Diagram:
	  return packageDiagramIcon;
	case UML_Use_Case_Diagram:
	  return useCaseDiagramIcon;
	case UML_State_Diagram:
	  return stateDiagramIcon;
	case UML_Sequence_Diagram:
	  return sequenceDiagramIcon;
	case UML_Activity_Diagram:
	  return activityDiagramIcon;
	default:
	  return docIcon;
	}
      case ProjectTreeNode.PROJECT_ROOT:
	return homeIcon;
      default:
      }
    } else if (value instanceof DrawShapeTreeNode) {
      DrawShapeTreeNode snode = (DrawShapeTreeNode) value;
      DrawShape shape = snode.getShape();
      if (shape != null) {
	if (shape instanceof TextShape) {
	  if (shape.getDisplayName() == null && 
	      shape.getTemplateName() == null) {
	    return textIcon;
	  } else {
	    return null;
	  }
	} else if (shape instanceof BoxLabel) {
	  BoxLabel label = (BoxLabel) shape;
	  return label.getIcon();
	} else {
	  Icon icon = getTemplateIcon(shape);
	  if (icon != null) {
	    return icon;
	  } else if (shape instanceof CompoundDrawShape) {
	    return folderIcon;
	  } else if (shape instanceof LineShape) {
	    return lineIcon;
	  } else if (shape instanceof RectangularShape) {
	    RectangularShape rshape = (RectangularShape) shape;
	    String iconName = rshape.getName() + "Icon";
	    return icons.get(iconName);
	  } else if (shape instanceof RoundRectangleShape) {
	    if (shape.isFilled()) {
	      return filledRoundRectIcon;
	    } else {
	      return roundRectIcon;
	    }
	  } else if (shape instanceof EllipseShape) {
	    if (shape.isFilled()) {
	      return filledOvalIcon;
	    } else {
	      return ovalIcon;
	    }
	  } else if (shape instanceof RectangleShape) {
	    shapeIcon.setShapeIcon(shape, 16, 16);
	    return shapeIcon;
	  } else if (shape instanceof QuadCurveShape) {
	    return arcIcon;
	  } else if (shape instanceof CubicCurveShape) {
	    return curveIcon;
	  } else if (shape instanceof CircleShape) {
	    return circleIcon;
	  } else if (shape instanceof PolygonShape) {
	    return polygonIcon;
	  } else if (shape instanceof PolyCurveShape) {
	    return polycurveIcon;
	  } else if (shape instanceof OrthogonalLineShape) {
	    return orthlineIcon;
	  } else if (shape instanceof PolyLineShape) {
	    return polylineIcon;
	  } else if (shape instanceof ScribbleShape) {
	    return scribbleIcon;
	  }
	}
      }
    } 
    return null;
  }

  protected Icon getTemplateIcon(DrawShape shape) {
    Icon icon = null;
    if (shape != null) {
      String t = shape.getTemplateName();
      if (t != null) {
	icon = icons.get(t);
      }
    }
    return icon;
  }

  protected ShapeIcon shapeIcon = new ShapeIcon();

}
